package jpm.numbertriangle;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;

import jpm.common.JPMException;
import jpm.common.JPMException.ErrorCode;
import jpm.common.ListOfListBuilder;

import org.junit.Test;

public class NumberTriangleTest {

	@Test
	public void ensureValid_invalidInput_throwsInvalidInput() {
		try {
			List<List<Integer>> listOfList = ListOfListBuilder.getBuilder()
					.append(1).append(1, 3).append(1, 2, 3).append(2).get();

			NumberTriangle.ensureValid(listOfList);
			assertTrue(false);
		} catch (JPMException ex) {
			assertEquals("invalid throws exception",
					ErrorCode.ERROR_INVALID_INPUT, ex.getErrorCode());
		}
	}

	@Test
	public void ensureValid_null_throwsAssertFail() {
		try {
			NumberTriangle.ensureValid(null);
		} catch (JPMException ex) {
			assertEquals(ErrorCode.ERROR_ASSERT_FAIL, ex.getErrorCode());
		}
	}

	@Test
	public void ensureValid_valid_doesNotThrow() {
		List<List<Integer>> listOfList = ListOfListBuilder.getBuilder()
				.append(1).append(1, 3).append(1, 2, 3).append(2, 3, 4, 5)
				.get();
		NumberTriangle.ensureValid(listOfList);
		// no exception
	}

	@Test
	public void getMaxTotal() {
		List<List<Integer>> listOfList = ListOfListBuilder.getBuilder()
				.append(1).get();

		assertEquals(1, NumberTriangle.getMaxSum(listOfList));

		listOfList = ListOfListBuilder.getBuilder().append(3).append(7, 4)
				.append(2, 4, 6).append(8, 5, 9, 3).get();
		assertEquals(23, NumberTriangle.getMaxSum(listOfList));
		
		listOfList = ListOfListBuilder.getBuilder().append(5).append(4, 6)
				.append(3, 8, 1).append(7, 2, 9, 5).get();
		assertEquals(28, NumberTriangle.getMaxSum(listOfList));
		
		listOfList = ListOfListBuilder.getBuilder().append(12).append(14, 9)
				.append(11, 7, 20).append(25, 13, 18, 35).append(30, 10, 6, 9, 40).get();
		assertEquals(116, NumberTriangle.getMaxSum(listOfList));			
	}
}
